//
//  JPVistorView.swift
//  JPWeibo
//
//  Created by 哇咔咔 on 16/9/20.
//  Copyright © 2016年 jp. All rights reserved.
//

import UIKit

class JPVistorView: UIView {
    
    //  MARK -- 懒加载控件
    lazy var houseImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var cycleImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var maskImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var tipLabel: UILabel = {
       
        let tip = UILabel()
        tip.numberOfLines = 0
        tip.textAlignment = .center
        tip.textColor = UIColor.gray
        tip.font = UIFont.systemFont(ofSize: 13)
        tip.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return tip
    }()
    
    lazy var loginButton: UIButton = {
        
        let login = UIButton()
        login.setTitle("登录", for: .normal)
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        login.setTitleColor(UIColor.darkGray, for: .normal)
        login.setTitleColor(UIColor.orange, for: .highlighted)
        login.adjustsImageWhenHighlighted = false
        login.addTarget(self, action: #selector(clickToLogin), for: .touchUpInside)
        
        return login
    }()
    
    lazy var registerButton: UIButton = {
        
        let register = UIButton()
        register.setTitle("注册", for: .normal)
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        register.setTitleColor(UIColor.darkGray, for: .normal)
        register.setTitleColor(UIColor.orange, for: .highlighted)
        register.adjustsImageWhenHighlighted = false
        register.addTarget(self, action: #selector(clickToRegister), for: .touchUpInside)
        
        return register
    }()
    
    var loginClosure: (() -> ())?
    
    var registerClosure: (() -> ())?
    
    //重载构造方法
    init(frame: CGRect, title: String?, imageName: String?) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(cycleImageV)
        addSubview(maskImageV)
        addSubview(houseImageV)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        cycleImageV.snp_makeConstraints(closure: { (make) in
            
            make.center.equalTo(self)
        })
        
        maskImageV.snp_makeConstraints(closure: { (make) in
            
            make.center.equalTo(cycleImageV)
        })
        
        houseImageV.snp_makeConstraints { (make) in
            
            make.center.equalTo(cycleImageV)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(cycleImageV.snp_bottom)
            make.centerX.equalTo(houseImageV)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(tipLabel.snp_leading)
            make.top.equalTo(tipLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(tipLabel.snp_trailing)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
        
        if title != nil && imageName != nil {
            tipLabel.text = title
            houseImageV.image = UIImage(named: imageName!)
            cycleImageV.isHidden = true
        }
        
        addAnimation()
    }
    
    func addAnimation() {
        
        let cycleAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        cycleAnimation.toValue = 2 * M_PI
        cycleAnimation.duration = 20
        cycleAnimation.repeatCount = MAXFLOAT
        cycleAnimation.isRemovedOnCompletion = false
        
        cycleImageV.layer.add(cycleAnimation, forKey: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func clickToLogin() {
        
        loginClosure?()
    }

    func clickToRegister() {
        
        registerClosure?()
    }
}
